<?php

class AnnoScolastico{
	
	private $ID;
	private $data_apertura;
	private $data_chiusura;
	private $descrizione;
	private $fine_quadrimestre;
	private $data_inizio_lezioni;
	private $data_termine_lezioni;
	private $holydays = array();
		
	function __construct($record){
		$this->ID = $record['id_anno'];
		$this->data_apertura = format_date($record['data_inizio'], SQL_DATE_STYLE, IT_DATE_STYLE, "/");
		$this->data_chiusura = format_date($record['data_fine'], SQL_DATE_STYLE, IT_DATE_STYLE, "/");
		$this->descrizione = $record['descrizione'];
		$this->fine_quadrimestre = format_date($record['fine_quadrimestre'], SQL_DATE_STYLE, IT_DATE_STYLE, "/");
		$this->data_inizio_lezioni = format_date($record['data_inizio_lezioni'], SQL_DATE_STYLE, IT_DATE_STYLE, "/");
		$this->data_termine_lezioni = format_date($record['data_termine_lezioni'], SQL_DATE_STYLE, IT_DATE_STYLE, "/");
		$this->holydays = split(",", $record['vacanze']);
	}
	
	public function set_holydays($days){
		if(is_array($days))
			$this->holydays = $days;
		else
			$this->holydays = explode(",", $days);
	}
	
	public function get_holydays (){
		return $this->holydays;
	}
	
	public function get_holydays_to_string(){
		return join(",", $this->holydays);
	}
	
	public function get_data_inizio_lezioni(){
		return $this->data_inizio_lezioni;
	}
	
	public function get_data_termine_lezioni(){
		return $this->data_termine_lezioni;
	}
	
	public function get_ID(){
		return $this->ID;
	}
		
	public function get_data_apertura(){
		return $this->data_apertura;
	}
	
	public function get_data_chiusura(){
		return $this->data_chiusura;
	}
	
	public function get_descrizione(){
		return $this->descrizione;
	}
	
	public function get_fine_quadrimestre(){
		return $this->fine_quadrimestre;
	}
	
	public function to_string(){
		return "Anno scolastico ".$this->descrizione;
	}

}

class Classe{
	private $ID;
	private $codice_argo;
	private $anno;
	private $sezione;
	private $sede;
	
	function __construct($record){
		$this->ID = $record['id_classe'];
		$this->codice_argo = $record['codice'];
		$this->sede = $record['sede_des'];
		$this->anno = $record['anno_corso'];
		$this->sezione = $record['sezione'];
	}
	
	public function get_ID(){
		return $this->ID;
	}
	
	public function get_codice_argo(){
		return $this->codice_argo;
	}
	
	public function get_anno(){
		return $this->anno;
	}
	
	public function get_sezione(){
		return $this->sezione;
	}
	
	public function get_sede(){
		return $this->sede;
	}
	
	public function to_string(){
		return "Classe ".$this->anno.", sezione ".$this->sezione." - ".$this->sede;
	}
	
}

class OraDiLezione{
	private $ID;
	private $ora;
	private $giorno;
	private $materia;
	private $compresenza;
	private $sostegno;
	private $classe;
	private $docente;
	private $descrizione;
	
	public function __construct($record){
		$this->ID = $record['id'];
		$this->ora = $record['ora'];
		$this->materia = $record['materia'];
		$this->compresenza = $record['materia2'];
		$this->sostegno = $record['sostegno'];
		$this->giorno = $record['giorno'];
		$this->classe = $record['classe'];
		$this->docente = $record['docente'];
		$this->descrizione = $record['descrizione'];
	}
	
	public function getMateria(){
		return $this->materia;
	}
	
	public function getID(){
		return $this->ID;
	}
	
	public function getClasse(){
		return $this->classe;
	}
	
	public function getOra(){
		return $this->ora;
	}
	
	public function getGiorno(){
		return $this->giorno;
	}
	
	public function getDescrizione(){
		return $this->descrizione;
	}
	
	public function getDocente(){
		return $this->docente;
	}
	
}

class Orario{
	private $orario;
	
	public function __construct(){
		$this->orario = array();
	}
	
	public function addHour($ora){
		$index = $ora->getID();
		$this->orario[$index] = $ora;
	}
	
	public function getHour($id){
		return $this->orario[$id];
	}
	
	public function searchHour($giorno, $ora, $classe){
		foreach($this->orario as $a){
			//print ($a->getClasse()."=".$classe."-----".$a->getOra()."=".$ora."----".$a->getGiorno()."=".$giorno."<br />");
			if(($a->getClasse() == $classe) && ($a->getOra() == $ora) && ($a->getGiorno() == $giorno)){
				return $a;
			}
		}
		print "Nessuna ora trovata";
		return 0;
	}
	
	public function searchMateria($id){
		return $this->getHour($id)->getMateria();
	}
	
	public function getMateria($classe, $giorno, $ora){
		$h = $this->searchHour($giorno, $ora, $classe);
		return $h->getMateria();
	}
	
	public function getDescrizione($classe, $giorno, $ora){
		$h = $this->searchHour($giorno, $ora, $classe);
		return $h->getDescrizione();
	}
	
	public function _count(){
		return count($this->orario);
	}
}

class User{
	/**
	* from table: utenti
	 */
	private $uid;
	private $firstName;
	private $lastName;
	private $username;
	private $groups;
	private $perms;
	/**
	 * from table: docenti
	 */
	private $subject;
	/**
	 * from table: cdc
	 * multidimensional array
	 * first dimension: id_class=>array
	 * second dimension: class=>class name; coordinator=>boolean; subjects=>array of ids
	 */
	private $classes = array();
	/**
	 * from table: profili
	 */
	private $birthday;
	private $address;
	private $phone;
	private $mobile;
	private $email;
	private $web;
	private $blog;
	private $messenger;

	public function __construct($u, $fn, $ln, $gr, $pr){
		$this->uid = $u;
		$this->firstName = $fn;
		$this->lastName = $ln;
		$this->groups = $gr;
		$this->perms = $pr;
	}

	public function setFirstName($fn){
		$this->firstName = $fn;
	}

	public function getFirstName(){
		return $this->firstName;
	}

	public function setLastName($ln){
		$this->lastName = $ln;
	}

	public function getLastName(){
		return $this->lastName;
	}

	public function getFullName($order = 1){
		($order == 1) ? ($ret = $this->firstName." ".$this->lastName) : ($ret = $this->lastName." ".$this->firstName);
		return $ret;
	}

	public function getPerms(){
		return $this->perms;
	}
	
	public function getUid(){
		return $this->uid;
	}

	public function getUsername(){
		return $this->username;
	}
	
	public function isInGroup($groupId){
		return in_array($groupId, $this->groups);
	}
	
	public function setSubject($subject){
		$this->subject = $subject;
	}
	
	public function getSubject(){
		return $this->subject;
	}
	
	public function getGroups(){
		return $this->groups;
	}
	
	public function setBirthday($day){
		$this->birthday = $day;
	}
	
	public function getBirthday(){
		return $this->birthday;
	}
	
	public function setAddress($add){
		$this->address = $add;
	}
	
	public function getAddress(){
		return $this->address;
	}
	
	public function setPhone($n){
		$this->phone = $n;
	}
	
	public function getPhone(){
		return $this->phone;
	}
	
	public function setMobile($n){
		$this->mobile = $n;
	}
	
	public function getMobile(){
		return $this->mobile;
	}
	
	public function setEmail($email){
		$this->email = $email;
	}
	
	public function getEmail(){
		return $this->email;
	}
	
	public function setWeb($web){
		$this->web = $web;
	}
	
	public function getWeb(){
		return $this->web;
	}
	
	public function setMessenger($m){
		$this->messenger = $m;
	}
	
	public function getMessenger(){
		return $this->messenger;
	}
	
	public function setBlog($b){
		$this->blog = $b;
	}
	
	public function getBlog(){
		return $this->blog;
	}
	
	public function setClasses($classes){
		$this->classes = $classes;
	}
	
	public function getClasses(){
		return $this->classes;
	}
	
	public function isCoordinator($id_classe){
		return $this->classes[$id_classe]['coordinatore'];
	}
	
	/**
	 * 
	 * updates all profile's fields using a database record
	 */
	public function setProfile($row){
		$this->birthday = format_date($row['data_nascita'], SQL_DATE_STYLE, IT_DATE_STYLE, "/");
		$this->address = $row['indirizzo'];
		$this->phone = $row['telefono'];
		$this->mobile = $row['cellulare'];
		$this->email = $row['email'];
		$this->messenger = $row['messenger'];
		$this->web = $row['web'];
		$this->blog = $row['blog'];
	}

	public function getGroupsString(){
		return join(",", $this->groups);
	}

	public function check_perms($permissions){
		return $this->perms&$permissions;
	}
	
	public function isTeacher(){
		return $this->check_perms(2);
	}
	
	public function isAdministrator(){
		return $this->check_perms(1);
	}
}

abstract class CustomException extends Exception{
	protected $message = 'Unknown exception';     // Exception message
	private $string;                              // Unknown
	protected $code = 0;                          // User-defined exception code
	protected $file;                              // Source filename of exception
	protected $line;                              // Source line of exception
	private $trace;                               // Unknown

	
	public function __construct($message, $code = 0){
		parent::__construct($message, $code);
	}
	 
	public function __toString(){
		return get_class($this) . " '{$this->message}' in {$this->file}({$this->line})\n" . "{$this->getTraceAsString()}";
	}
}

/*
* Handles SQL errors
* @author	 	Riccardo Bachis <cravenroad17@gmail.com>
* @created 	06/28/2011
* @last_mod 	06/28/2011
*
*/
class MySQLException extends CustomException{

	/**
	* the sql associated with the exception
	 */
	private $sql;

	/**
	* array of data about the error
	*/
	private $errors;
	
	public function __construct($message, $code, $query){
		parent::__construct($message, $code);
		if ($query != null){
			$this->sql = $query;
			$this->errors = array();
			$this->errors['data'] = date("d/m/Y");
			$this->errors['ora'] = date("H:i:s");
			$this->errors['ip_address'] = $_SERVER['REMOTE_ADDR'];
			$this->errors['referer'] = $_SERVER['HTTP_REFERER'];
			$this->errors['script'] = $_SERVER['SCRIPT_NAME'];
			$this->errors['query'] = $this->sql;
			$this->errors['error'] = $this->message;
			$_SESSION['__mysql_error__'] = $this->errors;
		}
	}

	/**
	* Convert the Exception to String
	*
	* @return String
	*/
	function __toString(){
		return "Si e' verificato un errore SQL nell'istruzione seguente: ".$this->sql.". Messaggio d'errore: ".$this->message;
	}
	
	/**
	* Convert the Exception to an html formatted string
	*
	* @return String
	*/
	function __toHTML(){
		return "<span>Si &egrave; verificato un errore SQL nell'istruzione seguente:</span> <span style='font-weight: bold'>$this->sql. </span><br />Messaggio d'errore: <span style='color: red'>".$this->message."</span>";
	}

	function redirect(){
		header("Location: http://www.inrich.it/rclasse/shared/mysql_error.php");
	}
	
	function alert(){
		$alert = '<script type="text/javascript">
				alert("Errore SQL.\nQuery: '.$this->getQuery().'\nErrore: '.$this->getMessage().'");
		window.close();
		</script>';
		print $alert;
	}
	
	function fake_alert(){
		print "ko|".$this->getMessage()."|".$this->getQuery();
		exit;
	}

	function getQuery(){
		return $this->sql;
	}
}

?>